import requests
import re
import pymysql
import pandas    #写入excl
import matplotlib.pyplot as plt   #可视化
import matplotlib as mpl        #配置字体
mpl.rcParams['font.sans-serif'] =['SimHei']

# 配置画图风格
plt.rcParams["axes.labelsize"] = 16.
plt.rcParams["xtick.labelsize"] = 14.
plt.rcParams["ytick.labelsize"] = 14.
plt.rcParams["legend.fontsize"] = 12.
plt.rcParams["figure.figsize"] = [15. , 6.]

db =pymysql.connect(
                          host='localhost',
                          port=3306,
                          user='root',
                          passwd='jingfei',
                          db='表格',
                          charset='UTF8'

)
cursor = db.cursor()

def getPageIndex():
    data ='User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)' \
          ' Chrome/49.0.2623.221 Safari/537.36 SE 2.X M'
    url = 'http://www.laoyuegou.com/x/zh-cn/lol/lol/godrank.html'
    response = requests.get(url, data=data)
    response.encoding = 'utf-8'
    html = response.text
#   print(html)
    reg = re.compile(r'<li val=.*?class="cn-li.*?">.*?<a href="(.*?)">.*?</a>', re.S)
    guoFuurl = re.findall(reg, html)

    return guoFuurl




def getPage(url):
   if url =='http://www.laoyuegou.com/x/zh-cn/lol/lol/godrank.html?region=cn&area=21':
         for page in range(1,5):
           data ='User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'
           url = url +'&page='+str(page)
   else:
       for page in range(1,8):
           data ='User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'
           url = url +'&page='+str(page)
   response = requests.get(url,data)
   response.encoding = 'utf-8'
   print(response.status_code)
   html = response.text
   print(html)
   reg = re.compile(r'<div class="player-server">(.*?)</div>.*?<span>(.*?)<em class="color-win">(.*?)</em></span>.*?<span>.*?</span>.*?<span>(.*?)<em class="color-defeat">(.*?)</em></span>'+
                            '.*?<div class="win" style="width:(.*?)"></div>.*?<span class="color-zhongdan">(.*?)</span></div>.*?<div class="item7 common-role">.*?alt="(.*?)".*?alt="(.*?)".*?alt="(.*?)"',re.S)
   image = re.findall(reg,html)
   print(image)
   for i in image:
           cursor.execute("insert into lol(lol_name,win,lol_defeat,win_style,lol_type,yingxiong1,yingxiong2,yingxiong3)values('{}','{}','{}','{}','{}','{}','{}','{}')".format(i[0],i[1],i[3],i[5],i[6],i[7],i[8],i[9]))
           db.commit()
   data1 = pandas.DataFrame(image)
   data1.to_csv(r'LOL.csv',header=False,index=False,mode='a&page+')


def main():
    url = getPageIndex()
    for url in url:
      # print(url)
       getPage(url)


if __name__ == '__main__':
     main()

'''
data = pandas.read_csv(r'LOL.csv',)

data =data[0:2100]
# data.head(8)
# data = data.describe()
# print(data)
#
# print(data.head)
data = data['所在战区'].value_counts().plot(kind = 'bar',rot = 0)  #可视化条形图
plt.show()
print(data)
'''